package com.yhb.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yhb.model.mq.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import me.edzh.spring.bootstrap.utils.JSONUtil;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MQMessage<T> {
    public static final String T_TEST                                  = "test";
    public static final String T_CALC_FLOW_SUM_UP                      = "calculateFlowSumUp";
    public static final String T_CALC_MONTHLY_FLOW_SUM_UP              = "calculateMonthlyFlowSumUp";
    public static final String T_START_CALC_SPLITTINGS                 = "startCalculateSplittings";
    public static final String T_START_OUTPUT_INCOME_REPORT            = "startOutputIncomeReport";
    public static final String T_START_OUTPUT_INCOME_SUMMARY_REPORT    = "startOutputIncomeSummaryReport";
    public static final String T_CALC_MONTHLY_ACTIVATE_BONUS           = "calculateMonthlyActivateBonus";
    public static final String T_OUTPUT_INCOME_REPORT                  = "outputIncomeReport";
    public static final String T_OUTPUT_INCOME_SUMMARY_REPORT          = "outputIncomeSummaryReport";
    public static final String T_SUM_UP_APPROXIMATELY_INCOME           = "sumUpApproximatelyIncome";

    public static final String T_CALC_FLOW_SPLITTINGS                  = "calculateFlowSplittings";
    public static final String T_QUERY_UNION_PAY_ORDER                 = "queryUnionPayOrder";
    public static final String T_QUERY_UNION_PAY_ACTIVE_REMIT          = "queryUnionPayActiveRemit";

    public static final String T_CHECK_VIP_VALID                       = "checkVipValid";
    public static final String T_SYNC_LESHUA_ORDERS                    = "syncLeshuaOrders";
    public static final String T_SYNC_LESHUA_MERCHANTS                 = "syncLeshuaMerchants";

    public static final String T_CALC_ACTIVE_USER                      = "calculateActiveUser";


    public static final String T_CHANNEL_EVENT_BUS                     = "channelEventBus";
    public static final String T_CLEAR_USER_RESERVED_BALANCE           = "clearUserReservedBalance";


    public static final String T_CHECK_INVITEE_CERTIFICATE             = "checkInviteeCertificate";
    public static final String T_CALLBACK_THIRD_FLOW                   = "callbackThirdFlow";


    private String type;
    private MQHeader header;
    private T content;

    @JsonIgnore
    public static MQMessage<?> fromJson(byte[] json) {
        Map<?, ?> obj = JSONUtil.parseJson(new String(json, StandardCharsets.UTF_8), HashMap.class);
        String type = obj.get("type").toString();
        Map<?, ?> content = (Map<?, ?>)obj.get("content");
        return MQMessage.builder()
                .type(type)
                .content(JSONUtil.parseJson(content, classes.get(type)))
                .build();
    }

    private static Map<String, Class<?>> classes = new HashMap<String, Class<?>>() {{
        put(T_TEST, TestMQModel.class);
        put(T_CALC_FLOW_SUM_UP, SumUpParam.class);
        put(T_CALC_MONTHLY_FLOW_SUM_UP, SumUpParam.class);
        put(T_START_CALC_SPLITTINGS, SumUpParam.class);
        put(T_CALC_FLOW_SPLITTINGS, SumUpParam.class);
        put(T_START_OUTPUT_INCOME_REPORT, SumUpParam.class);
        put(T_START_OUTPUT_INCOME_SUMMARY_REPORT, SumUpParam.class);
        put(T_CALC_MONTHLY_ACTIVATE_BONUS, SumUpParam.class);
        put(T_OUTPUT_INCOME_REPORT, SumUpParam.class);
        put(T_OUTPUT_INCOME_SUMMARY_REPORT, SumUpParam.class);
        put(T_SUM_UP_APPROXIMATELY_INCOME, SumUpParam.class);
        put(T_QUERY_UNION_PAY_ORDER, QueryUnionPayOrderParam.class);
        put(T_SYNC_LESHUA_ORDERS, TimeRangeParam.class);
        put(T_SYNC_LESHUA_MERCHANTS, TimeRangeParam.class);
        put(T_CHECK_VIP_VALID, SumUpParam.class);
        put(T_CALC_ACTIVE_USER, TimeRangeParam.class);
        put(T_CHANNEL_EVENT_BUS, ChannelEvent.class);
        put(T_QUERY_UNION_PAY_ACTIVE_REMIT, QueryUnionFlowParam.class);
        put(T_CLEAR_USER_RESERVED_BALANCE, SumUpParam.class);
        put(T_CHECK_INVITEE_CERTIFICATE, TimeRangeParam.class);
        put(T_CALLBACK_THIRD_FLOW, IdParam.class);
    }};

    @Data
    @Builder
    public static class MQHeader {
        private String relatedUserId;

    }
}
